import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Objects;

public class IdentifierTable {
    private final LinkedList<Identifier> table;

    public IdentifierTable() {
        table = new LinkedList<>();
    }

    public boolean existIdentifier(String name) {
        for (Identifier id : table) {
            if (Objects.equals(id.getName(), name))
                return true;
        }
        return false;
    }

    public boolean add(String name) {
        if (this.existIdentifier(name)) return false;
        this.table.addLast(new Identifier(name));
        return true;
    }

    public void clear() {
        table.clear();
    }

    public int getIndexByName(String name){
        int counter = 0;
        for(Identifier id : table){
            if (id.getName().equals(name)){
                return counter;
            }
            counter++;
        }
        return -1;
    }
    public boolean updateIdentifierTypeByName(String name, String type){
        int index = this.getIndexByName(name);
        if (index == -1) return false;
        int counter = 0;
        for(Identifier id : this.table){
            if (counter == index ) {
                if(!(type.equals(id.type))) {
                    id.type = type;
                    this.table.set(index, id);
                }
                else {
                    return false;
                }

                break;
            }
            counter++;
        }
        return true;
    }
    public boolean updateIdentifierValueByName(String name, String value){
        int index = this.getIndexByName(name);
        if (index == -1) return false;
        int counter = 0;
        for(Identifier id : this.table){
            if (counter == index) {
                id.value = value;
                this.table.set(index, id);
            }
            counter++;
        }
        return true;
    }
    public String getIdentifierValueByName(String name){
        int index = this.getIndexByName(name);
        if (index == -1) return "error identifier not exists";
        int counter = 0;
        String value = "";
        for(Identifier id : this.table){
            if (counter == index) {
                if(id.getValue() == null) {
                    System.out.println("标识符 "+ id.name +" 值未初始化！已默认为0");
                    return "0";
                }
                value = id.getValue();
            }
            counter++;
        }
        return value;
    }
    public int getIdentifierValueByNameAsInt(String name){
        int index = this.getIndexByName(name);
        if (index == -1) return -1;
        int counter = 0;
        String value = "";
        for(Identifier id : this.table){
            if (counter == index)
                value = id.getValue();
            counter++;
        }
        if(value.equals("")) return -1;
        return Integer.parseInt(value);
    }

    public String dump() {
        StringBuilder s = new StringBuilder();
        s.append("identifier table:\n");
        for (Identifier id : table) {
            s.append(id.getName()).append(", ").append(id.getType()).append(", ").append(id.getValue()).append(";\n");
        }
        return s.toString();
    }

}
